package com.tourlaid.mamagerment.service.impl

import com.tourlaid.mamagerment.common.Contant
import com.tourlaid.mamagerment.dto.StoreListDto
import com.tourlaid.mamagerment.entity.Store
import com.tourlaid.mamagerment.enums.StoreStateEnum
import com.tourlaid.mamagerment.enums.TldExceptionEnum
import com.tourlaid.mamagerment.form.StoreForm
import com.tourlaid.mamagerment.mapper.StoreMapper
import com.tourlaid.mamagerment.service.StoreService
import com.tourlaid.mamagerment.util.BosUtil
import com.tourlaid.mamagerment.util.LoginUtil
import com.tourlaid.mamagerment.util.TldException
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Service
import org.springframework.util.StringUtils
import javax.annotation.Resource

/**
 * Created by Administrator on 2017/8/16.
 */
@Service("stroreService")
class StoreServiceImpl : StoreService {
    override fun update(form: StoreForm) {
        if(form.id == null){
            return
        }
        if (StringUtils.isEmpty(form.storeName)) {
            throw TldException(TldExceptionEnum.STORENAME_NULL)
        }
        var image: String? = null
        if (!StringUtils.isEmpty(form.storeImage)) {
            image = BosUtil.PutObject(Contant.BUCKET_STORE, form.storeImage)
        }
        var newStore = Store()
        BeanUtils.copyProperties(form, newStore)
        newStore.storeState = StoreStateEnum.REVIEWING.code
        newStore.image = image
        newStore.editor = LoginUtil.userId
        storeMapper!!.update(newStore)
    }

    @Resource
    val storeMapper: StoreMapper? = null

    override fun queryForList(searchContent: String?, storeType: Int?, cityName: String?, area: String?,
                              district: String?): List<StoreListDto>? {
        return storeMapper!!.queryForList(searchContent, storeType, cityName, area, district)
    }

    override fun getById(id: Long): Store {
        return storeMapper!!.getById(id)
    }

    override fun queryHotStore(cityName: String): List<Store> {
        return storeMapper!!.queryHotStore(cityName)
    }

    override fun minusWhenThoughtDel(id: Long?) {
        id ?: return
        storeMapper!!.updateThoughtCount(id, -1)
    }

    override fun plusWhenThoughtIns(id: Long, count: Int) {
        storeMapper!!.updateThoughtCount(id, 1)
    }

    override fun insert(form: StoreForm): Long {
        if (StringUtils.isEmpty(form.storeName)) {
            throw TldException(TldExceptionEnum.STORENAME_NULL)
        }
        var image: String? = null
        if (!StringUtils.isEmpty(form.storeImage)) {
            image = BosUtil.PutObject(Contant.BUCKET_STORE, form.storeImage)
        }
        var newStore = Store()
        BeanUtils.copyProperties(form, newStore)
        newStore.storeState = StoreStateEnum.REVIEWING.code
        newStore.image = image
        newStore.creator = LoginUtil.userId
        newStore.editor = LoginUtil.userId
        storeMapper!!.insert(newStore)
        return newStore.id!!
    }
}